package com.mrjy.maintain.bo;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.entity.UserSchoolRel;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.maintain.dao.ISchoolInfoDao;
import com.mrjy.maintain.dao.IUserInfoDao;
import com.mrjy.maintain.dao.IUserSchoolRelDao;
import com.mrjy.maintain.dto.SchoolInfoDTO;

@Service("userSchoolRelBo")
public class UserSchoolRelBo implements IUserSchoolRelBo{
	
	@Resource
	private IUserSchoolRelDao userSchoolRelDao;

	@Resource
	private IUserInfoDao userInfoDao;
	
	@Resource
	private ISchoolInfoDao schoolInfoDao;
	
	

	@Override
	public Object txInsert(UserSchoolRel rel,HttpSession session) {
		Map<String , Object> map = new HashMap<String, Object>();
		rel.setCreateTime(new Timestamp(System.currentTimeMillis()));
		rel.setDisableFlag(0);
		
		UserInfo info = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == info){
			map.put("err_code", -1);
			map.put("err_msg", "当前用户回话失效");
			return map;
		}
		
		int count = userSchoolRelDao.insert(rel);
		if(count == 1){
			map.put("err_code", 0);
			map.put("err_msg", "插入成功");
			info.setBelongSchoolId(rel.getSchoolId());
			session.setAttribute(GlobalConstant.USER_INFO,info);
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "插入失败")	;
		}
		return map ;
		
	}
	@Override
	public Object queryById(int id) {
		Map<String, Object> map = new HashMap<String,Object>();
		UserSchoolRel rel = userSchoolRelDao.queryById(id);
		if(rel != null){
			map.put("err_code", 0);
			map.put("err_msg", "查询成功");
			map.put("info", rel);
		}else{
			map.put("err_code", 1);
			map.put("err_msg", "无数据");
		}
		return map;
	}
	@Override
	public Object updateById(UserSchoolRel rel) {
		Map<String , Object> map = new HashMap<String, Object>();
		int count = userSchoolRelDao.updateById(rel);
		if(count == 1){
			map.put("err_code", 0);
			map.put("err_msg", "更新成功");
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "更新失败")	;
		}
		return map ;
	}
	@Override
	public int deleteByIds(int[] ids) {
		try{
			return userSchoolRelDao.deleteByIds(ids);
		}catch(DataAccessException  e){
			return 0;
		}
	}
	@Override
	public int updateDisableFlag(UserSchoolRel rel) {
		return userSchoolRelDao.updateDisableFlag(rel);
	}
	@Override
	public Object queryByUserIdAndSchoolId(UserSchoolRel rel) {
		Map<String , Object> map = new HashMap<String, Object>();
		if(rel.getUserId() == null){
			map.put("err_code", 1);
			map.put("err_msg", "用户id为空");
			return map ;
		}
		if(rel.getSchoolId() == null){
			map.put("err_code", 2);
			map.put("err_msg", "学校id为空");
			return map ;
		}
		
		UserSchoolRel info = userSchoolRelDao.queryByUserIdAndSchoolId(rel);
		
		SchoolInfoDTO dto = schoolInfoDao.queryById(rel.getSchoolId());
		boolean isDialog = false;
		
		if(dto != null && info != null){
			//先判断三级联动   下拉信息权限（0：孩子学校；1：孩子班级）保存时是拼接的,如：0;1
			if("1".equals(dto.getThreeLinkageFlag())){//选择了三级联动
				if(dto.getPullInfoAuth().indexOf("0") >= 0 && StringUtils.isBlank(info.getStudentSchool())){
					isDialog = true ;
				}
				if(dto.getPullInfoAuth().indexOf("1") >= 0 && StringUtils.isBlank(info.getStudentGrade())){
					isDialog = true ;
				}
			}
			
			//必填信息权限（0：孩子学号；1：孩子姓名；2：孩子学校；3：孩子班级;4:手机号码）保存时是拼接的,如：1;2
			if(StringUtils.isNotBlank(dto.getInfoAuth())){
				if(dto.getInfoAuth().indexOf("0") >= 0 && StringUtils.isBlank(info.getStudentNum())){
					isDialog = true;
				}
				if(dto.getInfoAuth().indexOf("1") >= 0 && StringUtils.isBlank(info.getStudentName())){
					isDialog = true;
				}
				if(dto.getInfoAuth().indexOf("2") >= 0 && StringUtils.isBlank(info.getStudentSchool())){
					isDialog = true;
				}
				if(dto.getInfoAuth().indexOf("3") >= 0 && StringUtils.isBlank(info.getStudentGrade()) && StringUtils.isBlank(info.getStudentClass())){
					isDialog = true;
				}
				if(dto.getInfoAuth().indexOf("4") >= 0 && StringUtils.isBlank(info.getStudentMobilePhone())){
					isDialog = true;
				}
			}
		}
		
		
		
		if(info != null){
			map.put("err_code", 0);
			map.put("err_msg", "查询成功");
			map.put("info", info);
			map.put("isDialog", isDialog);
		}else {
			map.put("err_code", 3);
			map.put("err_msg", "无数据");
			map.put("info", null);
		}
		
		return map;
	}

}
